<?php

// 红包管理
namespace app\console\controller;

use think\Db;
use think\Url;
use think\Loader;
use think\Request;
use think\Controller;
use app\console\model\Member as MemberModel;
use app\console\model\RedPacket as RedPacketModel;
use app\console\model\MemberPacket as MemberPacketModel;
use app\console\model\RedPacketRule as RedPacketRuleModel;

class RedPacket extends Controller
{

    /**
     * 系统红包列表
     * @param Request $request
     * @param RedPacketModel $redPacket
     * @return mixed
     */
    public function index(Request $request, RedPacketModel $redPacket)
    {
        // 获取参数
        $inquire = $request->instance()->param();

        // 搜索条件
        $condition['id'] = ['neq', '0'];

        if (!empty($inquire['title'])) $condition['title'] = ['like', '%' . $inquire['title'] . '%'];

        $data = $redPacket->where($condition)->order(['id' => 'desc'])->paginate(10);


        return $this->fetch('index', [
            'mate_title' => '系统红包列表',
            'data'       => $data
        ]);
    }


   public function rule()
   {
       if (Request::instance()->isPost()) {

           $data = Request::instance()->param();

           $dispose = (new RedPacketModel())->allowField(true)->save($data);

           if ($dispose) {
               win('操作成功', Url::build('RedPacket/index'));
           }

           fail('操作失败');
       }


       return $this->fetch('',[
           'mate_title' => '系统红包列表',
       ]);
   }


   public function all()
   {
       $rid = Request::instance()->param('pid');

       $data = (new RedPacketModel())->where('pid',$rid)->select();


       return $this->fetch('',[
          'mate_title' => '红包列表',
          'data'       => $data,
           'rid'       => $rid,
       ]);
   }



   public function pack()
   {
       $pid = Request::instance()->param('pid');

       if (Request::instance()->isPost()) {

           $data = Request::instance()->param();
           $data['pid'] =  $pid;

           $operation = (new RedPacketModel())->allowField(true)->save($data);

           if ($operation) {
               win('操作成功', Url::build('RedPacket/index'));
           }

           fail('操作失败');
       }


       return $this->fetch('',[
           'mate_title' => '创建红包',
           'pid'        => $pid,
       ]);
   }


   public function pick($id)
   {
       if (Request::instance()->isPost()) {

           $data = Request::instance()->param();


           $operation = (new RedPacketModel())->allowField(true)->save($data,['id'=>$id]);

           if ($operation) {
               win('操作成功', Url::build('RedPacket/all',['pid'=>$data['pid']]));
           }

           fail('操作失败');
       }

       $item = RedPacketModel::get($id);


       return $this->fetch('pack',[
           'mate_title' => '创建红包',
           'item'       => $item,
           'pid'        => $item['pid']
       ]);
   }


   public function pick_destory($id,$pid)
   {
       $redPacket = new RedPacketModel();
       if ($redPacket->destroy($id)) {

           win('操作成功', Url::build('RedPacket/all',['pid'=>$pid]));

       }

       fail('操作失败');
   }




    /**
     * 新增红包
     * @param Request $request
     * @param RedPacketModel $redPacket
     * @return mixed
     */
    public function create(Request $request, MemberPacketModel $redPacket)
    {

        if ($request->instance()->isPost()) {

            $data = $request->instance()->param();
            if($data['actual_price'] > $data['virtual_price']) fail('满减金额大于使用金额');
            // 验证
            // $validate = Loader::validate('RedPacket');
            // if (!$validate->scene('create')->check($data)) fail($validate->getError());

            // 提交数据
            // $dispose = $redPacket->allowField(true)->save($data);
            $data['create_time'] = time();
            $data['status'] = 2;
            $dispose = $redPacket->allowField(true)->save($data);


            if ($dispose) {
                win('操作成功', Url::build('MemberPacket/index'));
            }

            fail('操作失败');

        }

        $data = (new MemberModel())->where('status', 1)->select();


        return $this->fetch('create', [
            "mate_title" => '新增红包',
            'data' => $data
        ]);

    }


    /**
     * 编辑系统红包
     * @param $id
     * @param Request $request
     * @param RedPacketModel $redPacket
     * @return mixed
     */
    public function update($id, Request $request, RedPacketModel $redPacket)
    {

        if ($request->instance()->isPost()) {

            $data = $request->instance()->param();
            if($data['actual_price'] > $data['virtual_price']) fail('满减金额大于使用金额');
            // 验证
            $validate = Loader::validate('RedPacket');
            if (!$validate->scene('create')->check($data)) fail($validate->getError());

            // 提交数据
            $dispose = $redPacket->allowField(true)->save($data, ['id' => $id]);

            if ($dispose) {
                win('操作成功', Url::build('RedPacket/index'));
            }

            fail('操作失败');

        }


        $item = RedPacketModel::get($id);


        return $this->fetch('create', [
            "mate_title" => '编辑系统红包',
            'item'       => $item

        ]);
    }


    /**
     * 删除红包
     * @param $id
     * @param RedPacketModel $redPacket
     */
    public function destroy($id, RedPacketModel $redPacket)
    {

        if ($redPacket->destroy($id)) {

            win('操作成功', Url::build('RedPacket/index'));

        }

        fail('操作失败');

    }

    public function aa()
    {

        $total_amount = Request::instance()->param('a');
        $ini = [
            'exp_a' => ['elt',$total_amount],
            'exp_b' =>  ['gt',$total_amount],
        ];
        $cc = Db::name('red_packet_rule')->where($ini)->value('id');

        $find = Db::name('red_packet')->where('pid',$cc)->select();


        $memp = [];
        foreach ($find as $value) {
            $memp[] = [
                'member_id' => 1,
                'packet_id' => $value['id'],
                'title'     => $value['title'],
                'actual_price' => $value['actual_price'],
                'virtual_price' => $value['virtual_price'],
                'content'     => $value['content'],
                'start_time'  => $value['start_time'],
                'end_time'   => $value['end_time'],
                'status'    => 2,
                'create_time' => time(),
            ];
        }

        //
        Db::name('member_packet')->insertAll($memp);
    }


    public function rull($id)
    {
        if (Request::instance()->isPost()) {

            $data = Request::instance()->param();

            $dispose = (new RedPacketModel())->allowField(true)->save($data,['id'=>$id]);

            if ($dispose) {
                win('操作成功', Url::build('RedPacket/index'));
            }

            fail('操作失败');
        }

        $item = RedPacketModel::get($id);

        return $this->fetch('rule',[
            'mate_title' => '系统红包列表',
            'item'       => $item,
        ]);
    }

    /**
     * 更改红包状态
     * @param RedPacketModel $redPacket
     * @return bool|int
     */
    public function auditing(RedPacketModel $redPacket)
    {
        $id = Request::instance()->param('id');

        $status = $redPacket->where(['id' => $id])->value('status') == '1' ? 2 : 1;

        if ($redPacket->where(['id' => $id])->update(['status' => $status])) {
            return $status;
        }
        return false;
    }
}